﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Web.Utility;
using System.Data;
using BySoft.Server.Business;
using Aspose.Cells;
using BySoft.Server.Common;
using System.IO;

namespace BySoft.Web.Site.ComPage
{
    public partial class ExportExcel : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string strMsg = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("msg").Trim());
                string[] strs = strMsg.Split(',');
                switch (strs[0])
                {
                    case "BomList":
                        ExportBomList(strs);
                        break;
                    case "OtherInTo":
                        ExportOtherInTo(strs);
                        break;
                    case "OutInStockDetailList":
                        ExportOutInStockDetailList(strs);
                        break;
                    case "OutInStockTotalList":
                        ExportOutInStockTotalList(strs);
                        break;
                    case "StockTotalList":
                        ExportStockTotalList(strs);
                        break;
                    case "PurchaseReceive":
                        ExportPurchaseReceive(strs);
                        break;
                    case "SendWorkList":
                        ExportWorkList(strs);
                        break;
                    default:
                        break;
                }
            }
        }

        private void ExportBomList(string[] strs)
        {
            if (Session["DtBomListExport"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtBomListExport"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportBomList(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportBomList(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\BomList.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;

            int index = 1;
            int iStaLine = 0;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;
                    cells[iStaLine + index, 8].Style = styleDetail;
                    cells[iStaLine + index, 9].Style = styleDetail;


                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(index));
                    if (CommonMethod.GetString(item["PartsType"]) == "1")
                    { 
                        cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["ParentPartsID"]));
                    }
                    else {
                        cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["PartsID"]));
                    }
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["PartsName"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["PartsID"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["Material"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["Unit"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["PartsNum"]));
                    cells[iStaLine + index, 8].PutValue(CommonMethod.GetString(item["Brand"]));
                    cells[iStaLine + index, 9].PutValue(CommonMethod.GetString(item["Remark"]));
                    index++;
                }
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }

        private void ExportOtherInTo(string[] strs)
        {
            if (Session["DtOtherInToExport"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtOtherInToExport"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportOtherInTo(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportOtherInTo(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\OtherIn.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Left;
            //styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;

            Aspose.Cells.Style styleDetailFoot = excel.Styles[excel.Styles.Add()];
            styleDetailFoot.HorizontalAlignment = TextAlignmentType.Left;
            styleDetailFoot.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetailFoot.BackgroundColor = System.Drawing.Color.White;

            int index = 1;
            int iStaLine = 7;
            int iStaLineFoot = 3;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                cells[1, 0].PutValue("苏州汇科"+DateTime.Now.ToString("yyyy"));
                cells[4, 1].PutValue(dtHead.Rows[0]["SupplierName"].ToString().Trim());
                cells[4, 4].PutValue(dtHead.Rows[0]["OtherInStockID"].ToString().Trim());
                cells[4, 7].PutValue(dtHead.Rows[0]["WriteDate"].ToString().Trim());
                cells[5, 1].PutValue(dtHead.Rows[0]["ProjectID"].ToString().Trim());
                cells[5, 4].PutValue(dtHead.Rows[0]["Operator"].ToString().Trim());
                cells[6, 1].PutValue(dtHead.Rows[0]["Remark"].ToString().Trim());
                decimal dNum = 0;
                decimal dAmount = 0;
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;
                    cells[iStaLine + index, 8].Style = styleDetail;


                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(item["StandardPartsID"]));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["StandardPartsName"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["MaterialQuality"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["StockUnit"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["InNum"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["Price"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["Amount"]));
                    if(!string.IsNullOrEmpty(CommonMethod.GetString(item["InNum"])))
                    {
                        dNum+=Convert.ToDecimal(CommonMethod.GetString(item["InNum"]));
                    }
                    if (!string.IsNullOrEmpty(CommonMethod.GetString(item["Amount"])))
                    {
                        dAmount += Convert.ToDecimal(CommonMethod.GetString(item["Amount"]));
                    }
                    index++;
                }

                cells[iStaLine + index + iStaLineFoot, 0].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 1].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 2].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 3].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 4].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 5].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 6].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 7].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 8].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 0].PutValue(CommonMethod.GetString("页脚"));
                cells[iStaLine + index + iStaLineFoot, 6].PutValue("数量合计：");
                cells[iStaLine + index + iStaLineFoot, 7].PutValue(dNum.ToString());
                cells[iStaLine + index + iStaLineFoot+1, 6].PutValue("金额合计：");
                cells[iStaLine + index + iStaLineFoot + 1, 7].PutValue(dAmount.ToString());
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }


        private void ExportOutInStockDetailList(string[] strs)
        {
            if (Session["DtOutInStockDetailList"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtOutInStockDetailList"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportOutInStockDetailList(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportOutInStockDetailList(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\OutInStockDetailList.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;

            int index = 1;
            int iStaLine = 0;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;
                    cells[iStaLine + index, 8].Style = styleDetail;
                    cells[iStaLine + index, 9].Style = styleDetail;
                    cells[iStaLine + index, 10].Style = styleDetail;
                    cells[iStaLine + index, 11].Style = styleDetail;
                    cells[iStaLine + index, 12].Style = styleDetail;
                    cells[iStaLine + index, 13].Style = styleDetail;
                    cells[iStaLine + index, 14].Style = styleDetail;
                    cells[iStaLine + index, 15].Style = styleDetail;

                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(item["ProjectID"]));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["StandardPartsTypeID"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["StandardPartsTypeName"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["PartsID"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["PartsName"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["MaterialQuality"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["WriteDate"]));
                    cells[iStaLine + index, 8].PutValue(CommonMethod.GetString(item["FormName"]));
                    cells[iStaLine + index, 9].PutValue(CommonMethod.GetString(item["FormID"]));
                    cells[iStaLine + index, 10].PutValue(CommonMethod.GetString(item["StockUnit"]));
                    cells[iStaLine + index, 11].PutValue(CommonMethod.GetString(item["StockName"]));
                    cells[iStaLine + index, 12].PutValue(CommonMethod.GetString(item["BeginNum"]));
                    cells[iStaLine + index, 13].PutValue(CommonMethod.GetString(item["BeginCost"]));
                    cells[iStaLine + index, 14].PutValue(CommonMethod.GetString(item["TransactionNum"]));
                    cells[iStaLine + index, 15].PutValue(CommonMethod.GetString(item["Cost"]));
                    index++;
                }
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }

        private void ExportOutInStockTotalList(string[] strs)
        {
            if (Session["DtPartsTotal"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtPartsTotal"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportOutInStockTotalList(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportOutInStockTotalList(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\OutInStockTotalList.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;

            int index = 1;
            int iStaLine = 0;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                decimal dInStockCost = 0;
                decimal dOutStockCost = 0;
                decimal dBalancesStockCost = 0;
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;
                    cells[iStaLine + index, 8].Style = styleDetail;
                    cells[iStaLine + index, 9].Style = styleDetail;
                    cells[iStaLine + index, 10].Style = styleDetail;
                    cells[iStaLine + index, 11].Style = styleDetail;
                    cells[iStaLine + index, 12].Style = styleDetail;
                    cells[iStaLine + index, 13].Style = styleDetail;
                    cells[iStaLine + index, 14].Style = styleDetail;
                    cells[iStaLine + index, 15].Style = styleDetail;
                    cells[iStaLine + index, 16].Style = styleDetail;

                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(item["ProjectID"]));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["StandardPartsTypeID"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["StandardPartsTypeName"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["PartsID"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["PartsName"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["MaterialQuality"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["StockUnit"]));
                    cells[iStaLine + index, 8].PutValue(CommonMethod.GetString(item["InStockNum"]));
                    cells[iStaLine + index, 9].PutValue(CommonMethod.GetString(item["InStockPrice"]));
                    cells[iStaLine + index, 10].PutValue(CommonMethod.GetString(item["InStockCost"]));
                    cells[iStaLine + index, 11].PutValue(CommonMethod.GetString(item["OutStockNum"]));
                    cells[iStaLine + index, 12].PutValue(CommonMethod.GetString(item["OutStockPrice"]));
                    cells[iStaLine + index, 13].PutValue(CommonMethod.GetString(item["OutStockCost"]));
                    cells[iStaLine + index, 14].PutValue(CommonMethod.GetString(item["BalancesStockNum"]));
                    cells[iStaLine + index, 15].PutValue(CommonMethod.GetString(item["BalancesStockPrice"]));
                    cells[iStaLine + index, 16].PutValue(CommonMethod.GetString(item["BalancesStockCost"]));
                    index++;
                    if (item["InStockCost"].ToString() != "")
                    {
                        dInStockCost += Convert.ToDecimal(item["InStockCost"].ToString());
                    }
                    if (item["OutStockCost"].ToString() != "")
                    {
                        dOutStockCost += Convert.ToDecimal(item["OutStockCost"].ToString());
                    }
                    if (item["BalancesStockCost"].ToString() != "")
                    {
                        dBalancesStockCost += Convert.ToDecimal(item["BalancesStockCost"].ToString());
                    }
                }
                cells[iStaLine + index, 0].Style = styleDetail;
                cells[iStaLine + index, 1].Style = styleDetail;
                cells[iStaLine + index, 2].Style = styleDetail;
                cells[iStaLine + index, 3].Style = styleDetail;
                cells[iStaLine + index, 4].Style = styleDetail;
                cells[iStaLine + index, 5].Style = styleDetail;
                cells[iStaLine + index, 6].Style = styleDetail;
                cells[iStaLine + index, 7].Style = styleDetail;
                cells[iStaLine + index, 8].Style = styleDetail;
                cells[iStaLine + index, 9].Style = styleDetail;
                cells[iStaLine + index, 10].Style = styleDetail;
                cells[iStaLine + index, 11].Style = styleDetail;
                cells[iStaLine + index, 12].Style = styleDetail;
                cells[iStaLine + index, 13].Style = styleDetail;
                cells[iStaLine + index, 14].Style = styleDetail;
                cells[iStaLine + index, 15].Style = styleDetail;
                cells[iStaLine + index, 16].Style = styleDetail;

                cells[iStaLine + index , 2].PutValue("合计");
                cells[iStaLine + index, 10].PutValue(CommonMethod.GetString(dInStockCost));
                cells[iStaLine + index , 13].PutValue(CommonMethod.GetString(dOutStockCost));
                cells[iStaLine + index , 16].PutValue(CommonMethod.GetString(dBalancesStockCost));
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }

        private void ExportStockTotalList(string[] strs)
        {
            if (Session["DtStockTotalList"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtStockTotalList"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportStockTotalList(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportStockTotalList(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\StockTotalList.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;

            int index = 1;
            int iStaLine = 5;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                decimal dStockNum = 0;
                decimal dAmount = 0;
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;


                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(index));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["ProductID"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["ProductName"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["StockNum"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(""));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(""));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(""));
                    index += 1;
                }
                cells[iStaLine + index, 1].PutValue("核准：");
                cells[iStaLine + index, 3].PutValue(CommonMethod.GetString("復盤人:"));
                cells[iStaLine + index, 6].PutValue(CommonMethod.GetString("盤點人："));
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }


        private void ExportPurchaseReceive(string[] strs)
        {
            if (Session["DtPurchaseReceiveEx"] != null)
            {
                DataTable dtHead = (DataTable)Session["DtPurchaseReceiveEx"];
                if (dtHead == null || dtHead.Rows.Count < 1)
                {
                    return;
                }

                Response.Clear();
                Response.Buffer = false;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;

                Stream stream = Response.OutputStream;

                ExeExportPurchaseReceive(dtHead, stream);
                Response.End();

            }

        }

        private void ExeExportPurchaseReceive(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\PurchaseReceive.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;

            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            //styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;
            styleDetail.Font.Size = 10;

            Aspose.Cells.Style styleDetailFoot = excel.Styles[excel.Styles.Add()];
            styleDetailFoot.HorizontalAlignment = TextAlignmentType.Right;
            styleDetailFoot.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetailFoot.BackgroundColor = System.Drawing.Color.White;
            styleDetailFoot.Font.Size = 10;
            int index = 1;
            int iStaLine = 7;
            int iStaLineFoot = 2;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                cells[1, 0].PutValue("苏州汇科" + DateTime.Now.ToString("yyyy"));
                cells[4, 1].PutValue(dtHead.Rows[0]["SupplierName"].ToString().Trim());
                cells[4, 4].PutValue(dtHead.Rows[0]["PurchaseReceiveID"].ToString().Trim());
                cells[4, 8].PutValue(dtHead.Rows[0]["ReceiveDate"].ToString().Trim());
                cells[5, 1].PutValue(dtHead.Rows[0]["ProjectID"].ToString().Trim());
                cells[5, 4].PutValue(dtHead.Rows[0]["UserName"].ToString().Trim());
                cells[6, 1].PutValue(dtHead.Rows[0]["Remark"].ToString().Trim());
                decimal dAmount = 0;
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;
                    cells[iStaLine + index, 8].Style = styleDetail;
                    cells[iStaLine + index, 9].Style = styleDetail;

                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(item["PartsID"]));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["PartsName"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["Material"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["ParentPartsName"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["ReceiveNum"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["Unit"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["Price"]));
                    cells[iStaLine + index, 8].PutValue(CommonMethod.GetString(item["Amount"]));
                    cells[iStaLine + index, 9].PutValue(CommonMethod.GetString(item["StockName"]));

                    if (!string.IsNullOrEmpty(CommonMethod.GetString(item["Amount"])))
                    {
                        dAmount += Convert.ToDecimal(CommonMethod.GetString(item["Amount"]));
                    }
                    index++;
                }

                cells[iStaLine + index + iStaLineFoot, 0].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 1].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 2].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 3].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 4].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 5].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 6].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 7].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 8].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 9].Style = styleDetailFoot;
                cells[iStaLine + index + iStaLineFoot, 0].PutValue(CommonMethod.GetString("页脚"));
                cells[iStaLine + index + iStaLineFoot , 7].PutValue("金额合计：");
                cells[iStaLine + index + iStaLineFoot, 8].PutValue(dAmount.ToString());
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }


        private void ExportWorkList(string[] strs)
        {

            //DataTable dtHead = BsSendgood.SelectReportList(strs[1].ToString());
            //if (dtHead == null || dtHead.Rows.Count < 1)
            //{
            //    return;
            //}

            //Response.Clear();
            //Response.Buffer = false;
            //Response.Charset = "GB2312";
            //Response.AppendHeader("Content-Disposition", "attachment;filename=Export.xls");
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //Response.ContentType = "application/ms-excel";
            //this.EnableViewState = false;

            //Stream stream = Response.OutputStream;

            //ExeExportWorkList(dtHead, stream);
            //Response.End();
        }

        private void ExeExportWorkList(DataTable dtHead, Stream stream)
        {
            string filename = this.Server.MapPath("..\\Module") + "\\SendWorkList.xls";
            Workbook excel = new Workbook();
            excel.Open(filename);
            Worksheet worksheet = excel.Worksheets[0];
            Cells cells = worksheet.Cells;



            Aspose.Cells.Style styleDetail = excel.Styles[excel.Styles.Add()];
            styleDetail.HorizontalAlignment = TextAlignmentType.Center;
            styleDetail.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetail.BackgroundColor = System.Drawing.Color.White;
            styleDetail.Font.Size = 10;

            Aspose.Cells.Style styleDetailFoot = excel.Styles[excel.Styles.Add()];
            styleDetailFoot.HorizontalAlignment = TextAlignmentType.Right;
            styleDetailFoot.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
            //styleDetail.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
            styleDetailFoot.BackgroundColor = System.Drawing.Color.White;
            styleDetailFoot.Font.Size = 10;
            int index = 1;
            int iStaLine = 3;
            int iStaLineFoot = 1;
            if (dtHead != null && dtHead.Rows.Count > 0)
            {
                cells[1, 7].PutValue(dtHead.Rows[0]["SendGoodID"].ToString().Trim());
                cells[2, 1].PutValue(dtHead.Rows[0]["SuppliersID"].ToString().Trim());
                cells[2, 5].PutValue("日期："+dtHead.Rows[0]["SendDate"].ToString().Trim());
                foreach (DataRow item in dtHead.Rows)
                {
                    cells[iStaLine + index, 0].Style = styleDetail;
                    cells[iStaLine + index, 1].Style = styleDetail;
                    cells[iStaLine + index, 2].Style = styleDetail;
                    cells[iStaLine + index, 3].Style = styleDetail;
                    cells[iStaLine + index, 4].Style = styleDetail;
                    cells[iStaLine + index, 5].Style = styleDetail;
                    cells[iStaLine + index, 6].Style = styleDetail;
                    cells[iStaLine + index, 7].Style = styleDetail;


                    cells[iStaLine + index, 0].PutValue(CommonMethod.GetString(item["OrderID"]));
                    cells[iStaLine + index, 1].PutValue(CommonMethod.GetString(item["ProductName"]));
                    cells[iStaLine + index, 2].PutValue(CommonMethod.GetString(item["Specifications"]));
                    cells[iStaLine + index, 3].PutValue(CommonMethod.GetString(item["Unit"]));
                    cells[iStaLine + index, 4].PutValue(CommonMethod.GetString(item["Num"]));
                    cells[iStaLine + index, 5].PutValue(CommonMethod.GetString(item["Price"]));
                    cells[iStaLine + index, 6].PutValue(CommonMethod.GetString(item["Amount"]));
                    cells[iStaLine + index, 7].PutValue(CommonMethod.GetString(item["Remark"]));
                   
                    index++;
                }
                int iFootIndex = iStaLine + index;
                if (iFootIndex < 19)
                {
                    iFootIndex = 19;
                }
                worksheet.Cells.Merge(iFootIndex + iStaLineFoot, 0, 1, 9);
                worksheet.Cells.Merge(iFootIndex + iStaLineFoot + 1, 0, 1, 9);
                cells[iFootIndex + iStaLineFoot, 0].PutValue("地址：常州市武进区前黄镇前庙公路1号                          电话：0519-86280676             传真：0519-86281696");
                cells[iFootIndex + iStaLineFoot + 1, 0].PutValue("运输单位（签字）：                 收货单位（签字）：                  送货单位（签字）：张小兰");
            }

            excel.Save(stream, FileFormatType.Excel2003);
        }
    }
}